
#include <bits/stdc++.h>
const int MAXN = 4e5 + 5;
using namespace std;
int mx[MAXN << 2];
#define lc ((x) << 1)
#define rc ((x) << 1 | 1)

void update(int x, int l, int r, int p, int d) {
  if (l == r) {
    mx[x] = d;
    return;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    update(lc, l, mid, p, d);
  else
    update(rc, mid + 1, r, p, d);
  mx[x] = max(mx[lc], mx[rc]);
}

int query(int x, int l, int r, int L, int R) {
  if (l == L && r == R)
    return mx[x];
  int mid = (l + r) >> 1;
  if (R <= mid)
    return query(lc, l, mid, L, R);
  else if (L > mid)
    return query(rc, mid + 1, r, L, R);
  else
    return std::max(query(lc, l, mid, L, mid),
                    query(rc, mid + 1, r, mid + 1, R));
}

vector<pair<int, int>> vec;
int n;
int ans[MAXN];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a > b)
      swap(a, b);
    vec.push_back({b, a});
  }
  sort(vec.begin(), vec.end());

  for (int i = 0; i < vec.size(); ++i) {
    auto [r, l] = vec[i];
    ans[i] = query(1, 1, 2 * n, l, 2 * n) + 1;
    update(1, 1, 2 * n, l, ans[i]);
  }

  for (int i = 0; i < vec.size(); ++i) {
    auto [r, l] = vec[i];
    ans[i] += query(1, 1, 2 * n, r, 2 * n);
  }

  int res = *max_element(ans, ans + n);
  printf("%d\n", res);
  return 0;
}